// 8.24.h
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <error.h>
#include <signal.h>

extern int errno;
extern int ECHILD;
extern void psignal(int signal, const char *str);
#define NCHILDREN 2
#define MAXLINE 80
char buf[MAXLINE];

int main()
{
    int status;
    pid_t pid;

    for (int i = 0; i < NCHILDREN; ++ i)
    {
        pid = fork();
        if (pid == 0)
            *(char *)main = 1;
    }

    while (pid = wait(&status) > 0)
    {
        if (WIFEXITED(status))
            printf("child %d terminated normally with exit status = %d\n", pid, WEXITSTATUS(status));
        else
            if (WIFSIGNALED(status))
            {
                sprintf(buf, "child %d terminated by signal %d: ", pid, WTERMSIG(status));
                psignal(WTERMSIG(status), buf);
            }
    }

    if (errno != ECHILD)
    {
        fprintf(stderr, "%s: %s\n", "wait error", strerror(errno));
        exit(0);
    }
    return 0;
}